package com.mybatis.hello;

import com.mybatis.pojo.Flysysuser;
import com.mybatis.pojo.TbContent;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.jdbc.SQL;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.slf4j.Logger;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @author Pretty·Boy
 * @version 1.0
 * @desc:
 * @date 2018/10/20 18:33
 */
public class HelloMybatis {

    public static void main(String args[]) {
        try {
            selectTest();
        } catch (IOException e) {
            e.printStackTrace();
        }
//         addUser();
        System.out.println("------------------> 删除用户");
//        deleteUser();
//        updateUser();
    }

    /**
     * 查询用户
     */
    private static void selectTest() throws IOException {
        SqlSession sqlSession = getSqlSession();

        TbContent content = sqlSession.selectOne("getUserById", 30);
        System.out.println(content);

        List<Flysysuser> userList = sqlSession.selectList("getUserByName", "吴");
        for (Flysysuser flysysuser : userList) {
            System.out.println(flysysuser);
        }
    }


    public static void addUser() throws IOException {
        SqlSession sqlSession = getSqlSession();
        Flysysuser flysysuser = new Flysysuser();
        flysysuser.setUsercode("001");
        flysysuser.setTelnum("123");
        flysysuser.setUsername("abc");
        flysysuser.setCompanyid("999");
        int addUser = sqlSession.insert("addUser", flysysuser);
//       手动提交事务
        sqlSession.commit();
//        关闭sqlSession
        sqlSession.close();
        System.out.println("addUser = " + addUser);

    }

    /**
     * 获取SqlSession
     */
    private static SqlSession getSqlSession() throws IOException {
        //创建SqlSessionFactoryBuilder
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        //使用Mybatis提供的Resource工具类，加载properties属性文件
        InputStream inputStream = Resources.getResourceAsStream("mybatis.xml");
        //创建SqlSession工厂
        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
        //获取SqlSession
        return sqlSessionFactory.openSession();
    }

    /**
     * 删除用户
     *
     * @author pretty_boy
     * @time 2018/12/16
     */
    public static void deleteUser() {
        SqlSession sqlSession = null;
        try {
            sqlSession = getSqlSession();
            sqlSession.delete("deleteUser", 999);
            sqlSession.commit();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (sqlSession != null) {
                sqlSession.close();
            }
        }
    }

    /**
     * 更新用户
     *
     * @author pretty_boy
     * @time 2018/12/16
     */
    public static void updateUser() {
        SqlSession sqlSession = null;
        try {
            sqlSession = getSqlSession();
            Flysysuser flysysuser = new Flysysuser();
            flysysuser.setUsername("ccc");
            flysysuser.setCompanyid("991");
            sqlSession.update("updateUser", flysysuser);
            sqlSession.commit();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (sqlSession != null) {
                sqlSession.close();
            }
        }
    }
}
